package cn.com.xinjinfu.pdoulcProduct.activity.FixedProduct;

import android.app.ActionBar;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.base.BaseActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.base.BaseWebActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.login.CheckMobileActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.recharge.BuyActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.user.ActCertification;
import cn.com.xinjinfu.pdoulcProduct.activity.user.MyInvestListAct;
import cn.com.xinjinfu.pdoulcProduct.adapter.HuankuanAdapter;
import cn.com.xinjinfu.pdoulcProduct.adapter.MenuItemAdapter;
import cn.com.xinjinfu.pdoulcProduct.common.ConstantValues;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.entity.InvestBills;
import cn.com.xinjinfu.pdoulcProduct.entity.TransferProductInfoBean;
import cn.com.xinjinfu.pdoulcProduct.entity.TransferProductInfoBean.Debt.Invest.Bid;
import cn.com.xinjinfu.pdoulcProduct.entity.TransferProductInfoBean.Debt.Invest.Bid.Repayment;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ActivityManager;
import cn.com.xinjinfu.pdoulcProduct.manager.JSONManager;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.manager.TitleManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
import cn.com.xinjinfu.pdoulcProduct.manager.UIManager;
import cn.com.xinjinfu.pdoulcProduct.utils.NewUtil;
import cn.com.xinjinfu.pdoulcProduct.utils.TextViewUtil;
import cn.com.xinjinfu.pdoulcProduct.view.BaseGridView;
import cn.com.xinjinfu.pdoulcProduct.view.ListViewForScrollView;
import cn.com.xinjinfu.pdoulcProduct.view.LoadStatusBox;
import cn.com.xinjinfu.pdoulcProduct.view.TimeDownView;

/**
 * 转让标详情（公共页）
 * 1.从首页 普通投资列表进入
 * 2.从  我的投资  列表进入
 * rettonStyle为1，从我的投资列表进入
 *
 * 主要逻辑靠bidStatus值进行判断
 * Created by android-adt on 2016/1/25.
 */
public class TransferProductInfo extends BaseActivity implements View.OnClickListener {
    private String investId, rettonStyle, bidId, titlename;
    private int bidStatus;
    private LinearLayout huikuanjihua, ll_borrower, ll_company, ll_house, ll_company_describe;
    private RelativeLayout rl_title1, zhuanrang_info, zhuan_mingxi;
    private Button bt_mairu;
    private User user;
    private LoadStatusBox loadbox;
    private ScrollView sv_include;
    private MenuItemAdapter saMenuItem;
    private RequestQueue requen;
    private ArrayList<InvestBills> list;
    private HuankuanAdapter adapter;
    private TimeDownView timeDownView;
    private TransferProductInfoBean transferProductInfoBean;
    private TextView tv_company_describe, tv_company_name, tv_company_ceo, tv_company_money, tv_company_time, tv_company_friends,
            tv_bidding_status, top_title_tv, tv_status2, textView1_cycle, tv_annual_yield, tv_time, tv_zonge01,
            tv_shengyu, textView2, tv_object_name, tv_nian_shouyilv, tv_rongzi_money, tv_danbao_organization, tv_huankuan_style, tv_borrower_name, tv_id_number,
            tv_sex, tv_age, tv_marital_status, tv_hetong, tv_error_hint, risk_info, tv_current_city, tv_property_located, tv_building_area, tv_evaluated_price;
    private PopupWindow popupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.assignment_info_refund);
        //初始化头布局
        TitleManager.showTitle(this, "", "");
        ActivityManager.getInstance().addActivity(this);
        findViewById(R.id.invest_details_mc).setVisibility(View.INVISIBLE);//在数据调用成功前默认不显示内容
        requen = Volley.newRequestQueue(this);
        list = new ArrayList<InvestBills>();
        user = AccountManager.getInstance().getUser();
        Intent intent = getIntent();
        bidId = intent.getStringExtra("bidId");
        investId = intent.getStringExtra("investId");
        /**从不同页面跳转过来的状态值，0代表从首页理财跳转，1代表从我的P兜跳转*/
        rettonStyle = intent.getStringExtra("rettonStyle");
//        L.d("rettonStyle**********" + rettonStyle + "bidStatus===========" + bidStatus + "bidId====" + bidId);
        initView();
        //新加功能  回款计划
        ListViewForScrollView list_huikuan = (ListViewForScrollView) findViewById(R.id.list_huikuan);
        adapter = new HuankuanAdapter(this);
        list_huikuan.setAdapter(adapter);
        adapter.setList(list);
    }

    public void initView() {
        loadbox = (LoadStatusBox) this.findViewById(R.id.loadStatusBox);
        loadbox.setOnClickListener(this);
        loadbox.loading();

        huikuanjihua = (LinearLayout) findViewById(R.id.huikuanjihua);
        tv_hetong = (TextView) findViewById(R.id.tv_hetong);
        tv_error_hint = (TextView) findViewById(R.id.tv_error_hint);
        top_title_tv = (TextView) findViewById(R.id.top_title_tv);
        rl_title1 = (RelativeLayout) findViewById(R.id.rl_title1);
        zhuan_mingxi = (RelativeLayout) findViewById(R.id.zhuan_mingxi);
        timeDownView = (TimeDownView) findViewById(R.id.timedownview);
        tv_bidding_status = (TextView) findViewById(R.id.tv_bidding_status);
        tv_status2 = (TextView) findViewById(R.id.tv_status2);
        textView1_cycle = (TextView) findViewById(R.id.textView1_cycle);
        textView2 = (TextView) findViewById(R.id.textView2);
        tv_annual_yield = (TextView) findViewById(R.id.tv_annual_yield);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_zonge01 = (TextView) findViewById(R.id.tv_zonge01);
        tv_shengyu = (TextView) findViewById(R.id.tv_shengyu);
        bt_mairu = (Button) findViewById(R.id.bt_mairu);

        ll_company_describe = (LinearLayout) findViewById(R.id.ll_company_describe);
        ll_borrower = (LinearLayout) findViewById(R.id.ll_borrower);
        ll_company = (LinearLayout) findViewById(R.id.ll_company);
        ll_house = (LinearLayout) findViewById(R.id.ll_house);


        tv_company_describe = (TextView) findViewById(R.id.tv_company_describe);
        tv_company_name = (TextView) findViewById(R.id.tv_company_name);
        tv_company_time = (TextView) findViewById(R.id.tv_company_time);
        tv_company_ceo = (TextView) findViewById(R.id.tv_company_ceo);
        tv_company_money = (TextView) findViewById(R.id.tv_company_money);
        tv_company_friends = (TextView) findViewById(R.id.tv_company_friends);
        risk_info = (TextView) findViewById(R.id.risk_info);

        tv_object_name = (TextView) findViewById(R.id.tv_object_name);
        tv_nian_shouyilv = (TextView) findViewById(R.id.tv_nian_shouyilv);
        tv_rongzi_money = (TextView) findViewById(R.id.tv_rongzi_money);
        tv_danbao_organization = (TextView) findViewById(R.id.tv_danbao_organization);
        tv_huankuan_style = (TextView) findViewById(R.id.tv_huankuan_style);

        tv_borrower_name = (TextView) findViewById(R.id.tv_borrower_name);
        tv_id_number = (TextView) findViewById(R.id.tv_id_number);
        tv_sex = (TextView) findViewById(R.id.tv_sex);
        tv_age = (TextView) findViewById(R.id.tv_age);
        tv_marital_status = (TextView) findViewById(R.id.tv_marital_status);
        tv_current_city = (TextView) findViewById(R.id.tv_current_city);

        tv_property_located = (TextView) findViewById(R.id.tv_property_located);
        tv_building_area = (TextView) findViewById(R.id.tv_building_area);
        tv_evaluated_price = (TextView) findViewById(R.id.tv_evaluated_price);
        /**************禁止ScrollView自动滑动到底部*********************/
        sv_include = (ScrollView) findViewById(R.id.sv_include);
        disableAutoScrollToBottom();
        /** 最下面的风控信息GridView*/
        BaseGridView gridview = (BaseGridView) findViewById(R.id.gridView);
        saMenuItem = new MenuItemAdapter(this);
        gridview.setAdapter(saMenuItem);

        /*****查看合同******/
        tv_hetong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPopupWindow();
                int[] location = new int[2];
                v.getLocationOnScreen(location);// 获得指定控件的坐标
                popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0],
                        location[1] + v.getHeight() + NewUtil.dip2px(TransferProductInfo.this, 1));
            }
        });
        zhuan_mingxi.setOnClickListener(this);

        zhuanrang_info = (RelativeLayout) findViewById(R.id.zhuanrang_info);
        zhuanrang_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Dialog dialog = new Dialog(TransferProductInfo.this, R.style.dialog_tran);
                dialog.setContentView(R.layout.dialog_zhaiquan);
                dialog.findViewById(R.id.img_search).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                dialog.show();
                dialog.setCanceledOnTouchOutside(true);
            }
        });
    }

    /**
     * 获取查看合同PopupWindow实例
     */
    private void getPopupWindow() {
        if (null != popupWindow) {
            popupWindow.dismiss();
            return;
        } else {
            initPopuptWindow();
        }
    }

    private void clossPopupWindow() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
            popupWindow = null;
        }
    }

    /**
     * 创建  查看合同PopupWindow
     */
    protected void initPopuptWindow() {
        // TODO Auto-generated method stub
        View view = getLayoutInflater().inflate(R.layout.popupwindow_hetong, null, false);
        Button borrow_hetong = (Button) view.findViewById(R.id.borrow_hetong);
        LinearLayout ll_zhuanrang = (LinearLayout) view.findViewById(R.id.ll_zhuanrang);
        if(transferProductInfoBean.getIsTranfer() > 0){
            ll_zhuanrang.setVisibility(View.VISIBLE);
        }else {
            ll_zhuanrang.setVisibility(View.GONE);
        }
        Button zhuanrang_hetong = (Button) view.findViewById(R.id.zhuanrang_hetong);
        Button jujian_xieyi = (Button) view.findViewById(R.id.jujian_xieyi);
        borrow_hetong.setOnClickListener(this);
        zhuanrang_hetong.setOnClickListener(this);
        jujian_xieyi.setOnClickListener(this);
        popupWindow = new PopupWindow(view, NewUtil.dip2px(this, 100.0f), ActionBar.LayoutParams.WRAP_CONTENT, true);
//        popupWindow.setAnimationStyle(R.style.AnimationFade);
        popupWindow.setOutsideTouchable(true);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                clossPopupWindow();
                return false;
            }
        });
    }

    public void upData() {
        if (transferProductInfoBean != null) {
            bidStatus = transferProductInfoBean.getDebt().getStatus();
            titlename = transferProductInfoBean.getDebt().getInvest().getBid().getTitle();
            top_title_tv.setText(titlename);
            Repayment repayment = transferProductInfoBean.getDebt().getInvest().getBid().getRepayment();
            String portionTransferAmount = TextViewUtil.getSumLength(transferProductInfoBean.getDebt().getPortionTransferAmount());//转让每份的价格
            int haveBuyPortionnums = transferProductInfoBean.getDebt().getHaveBuyPortionnums();//已经转让的分数
            int totalPortionNums = transferProductInfoBean.getDebt().getTotalPortionNums();//总的分数
//            String debtAmount = TextViewUtil.getSumLength(transferProductInfoBean.getDebt().getDebtAmount());//原始债权价值
            double priceRate = transferProductInfoBean.getDebt().getDebtAmount() - transferProductInfoBean.getDebt().getTransferPrice();//"折让/份"金额
            String transferPrice = TextViewUtil.getSumLength(transferProductInfoBean.getDebt().getTransferPrice());//转让的总价格

            SpannableString tSS = new SpannableString("已转出  " + haveBuyPortionnums + "/"
                    + totalPortionNums + " 份");
            tSS.setSpan(new ForegroundColorSpan(0xFFF77F96), 5, String.valueOf(haveBuyPortionnums).length() + 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            /*****************    转让人   *************/
            if (bidStatus == 2) {    //别人未购买时的状态
                textView1_cycle.setText(portionTransferAmount + " /份");
                rl_title1.setVisibility(View.VISIBLE);
                tv_status2.setVisibility(View.GONE);
                bt_mairu.setVisibility(View.VISIBLE);
                daoJiShi();
                if (user != null && user.getCurrUser().getName().equals
                        (transferProductInfoBean.getDebt().getInvest().getUser().getName())) {
                    /*********************转让者本人********************/
                    if (haveBuyPortionnums == 0) {
                        tv_bidding_status.setText("转让中");
                        bt_mairu.setText("取消转让");
                        bt_mairu.setBackgroundColor(0xFFF4B942);
                        bt_mairu.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                quxiaoZhuanrangRequest();
                            }
                        });
                    } else {
                        tv_bidding_status.setText(tSS);
                        bt_mairu.setText("转让中");
                        bt_mairu.setBackgroundColor(0xFFCCCCCC);
                    }
                    /*******增加转让明细功能，新加逻辑
                     * 用户可随时转让*******************/
                } else if (transferProductInfoBean.getInvest().getTransfer_status() == 0) {
                    if (transferProductInfoBean.getDebt().getInvest().getBid().getStatus() == 4 && transferProductInfoBean.isInvestUser()) {
                        rl_title1.setVisibility(View.GONE);
                        tv_status2.setVisibility(View.VISIBLE);
                        /**  回款计划  */
                        huiKuanJiHua();
                        tv_status2.setText("还款中");
                        tv_status2.setTextColor(0xFF6ED3D6);
                        /**底部按钮*/
                        zhuanButtonPanduan();
                    }
                } else {
                    /*********************购买人********************/
                    tv_bidding_status.setText(tSS);
                    bt_mairu.setText("立即购买");
                    bt_mairu.setBackgroundColor(0xFFF77F96);
                    bt_mairu.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            User user = AccountManager.getInstance().getUser();
                            if (user != null && user.getCurrUser().isLogged()) {
                                if (user.getCurrUser().getIdNumber() == null) {
                                    Intent intent0 = new Intent();
                                    intent0.putExtra("cer_style_falg", 1);
                                    intent0.setClass(TransferProductInfo.this, ActCertification.class);
                                    startActivity(intent0);
                                    ToastManager.showShort(TransferProductInfo.this, "请先进行实名认证！");
                                } else {
                                    Intent intent = new Intent(TransferProductInfo.this, BuyActivity.class);
                                    Bundle bundle = new Bundle();
                                    bundle.putInt("objBuy", 2);
                                    bundle.putString("Id", transferProductInfoBean.getDebt().getId() + "");
                                    bundle.putString("titlename", titlename);
                                    intent.putExtras(bundle);
                                    startActivity(intent);
                                }
                            } else {
                                startActivity(new Intent(TransferProductInfo.this, CheckMobileActivity.class));
                            }
                        }
                    });
                }
            } else if (bidStatus == 3) {
                rl_title1.setVisibility(View.GONE);
                tv_status2.setVisibility(View.VISIBLE);
                if (transferProductInfoBean.getDebt().getTotalPortionNums() > 0) {
                    if (user != null && user.getCurrUser().getName().equals
                            (transferProductInfoBean.getDebt().getInvest().getUser().getName())) {
                        textView1_cycle.setText(transferPrice);
                    } else {
                        textView1_cycle.setText(TextViewUtil.getSumLength(transferProductInfoBean.getDebt().getPortionTransferAmount()
                                * (transferProductInfoBean.getInvest().getAmount() / 100)));
                    }
                } else {
                    textView1_cycle.setText(transferPrice);
                }
                /**  回款计划  */
                huiKuanJiHua();
                if (transferProductInfoBean.getInvest().getTransfer_status() == -1) {
                    tv_status2.setText("已转出");
                    tv_status2.setTextColor(0xFF999999);
                    bt_mairu.setVisibility(View.GONE);
                } else if (transferProductInfoBean.getInvest().getTransfer_status() == 0) {
                    if (transferProductInfoBean.getDebt().getInvest().getBid().getStatus() == 4
                            && transferProductInfoBean.isInvestUser()) {
                        tv_status2.setText("还款中");
                        tv_status2.setTextColor(0xFF6ED3D6);
                        /**底部按钮*/
                        zhuanButtonPanduan();
                    } else if (transferProductInfoBean.getDebt().getInvest().getBid().getStatus() == 5) {
                        tv_status2.setText("已结束");
                        tv_status2.setTextColor(0xFF999999);
                        bt_mairu.setVisibility(View.GONE);
                    }
                }
            } else if (bidStatus == 4) {
                rl_title1.setVisibility(View.VISIBLE);
                tv_status2.setVisibility(View.GONE);
                textView1_cycle.setText(portionTransferAmount + " /份");
                tv_bidding_status.setText(tSS);
                daoJiShi();
                /**底部按钮*/
                bt_mairu.setVisibility(View.VISIBLE);
                if (haveBuyPortionnums == totalPortionNums) {
                    bt_mairu.setText("已满标");
                    bt_mairu.setBackgroundColor(0xFFCCCCCC);
                }
            }
            //转让明细 大于0显示
            if (transferProductInfoBean.getTransferParticulars() > 0) {
                zhuan_mingxi.setVisibility(View.VISIBLE);
            } else {
                zhuan_mingxi.setVisibility(View.GONE);
            }

            //公共信息
            Bid bid = transferProductInfoBean.getDebt().getInvest().getBid();
            int id = bid.getProductId();
            if (id == 7) {
                ll_company_describe.setVisibility(View.VISIBLE);
                ll_borrower.setVisibility(View.GONE);
                ll_company.setVisibility(View.VISIBLE);
                ll_house.setVisibility(View.GONE);
                tv_company_describe.setText("   " + bid.getDescription());
                tv_company_name.setText(bid.getCompanyName());
                String time = bid.getSetupTime();
                String show_id = time.substring(0, 2) + "**" + time.substring(4);
                tv_company_time.setText(show_id);
                tv_company_ceo.setText(bid.getCompanyLegal().substring(0, 1) + "**");
                tv_company_money.setText(bid.getRegisterCapital());
                tv_company_friends.setText(bid.getCooperation());

            } else if (id == 1 || id == 11) {
                ll_company_describe.setVisibility(View.GONE);
                ll_borrower.setVisibility(View.VISIBLE);
                ll_company.setVisibility(View.GONE);
                ll_house.setVisibility(View.VISIBLE);
            } else {
                ll_company_describe.setVisibility(View.GONE);
                ll_borrower.setVisibility(View.VISIBLE);
                ll_company.setVisibility(View.GONE);
                ll_house.setVisibility(View.GONE);
            }

            if (repayment.getId() == 2 || repayment.getId() == 3) {  //还款方式，按月付息,提前还款
                textView2.setText("预期年化收益率(约)");
                tv_annual_yield.setText(NumberFormat.getInstance().format(transferProductInfoBean.getDebt().getTakeTransApr()) + "%");
            } else {                                                  //等额本息
                textView2.setText("折让/份");
                tv_annual_yield.setText(NumberFormat.getInstance().format(priceRate));
            }
            if (transferProductInfoBean.getDebt().getTotalPortionNums() > 0) {
                if (user != null && user.getCurrUser().getName().equals
                        (transferProductInfoBean.getDebt().getInvest().getUser().getName())) {
                    tv_zonge01.setText(TextViewUtil.getSumLength(transferProductInfoBean.getDebt().getDebtAmount()));
                } else {
                    tv_zonge01.setText(TextViewUtil.getSumLength(transferProductInfoBean.getDebt().getPortionOriginalAmount()
                            * (transferProductInfoBean.getInvest().getAmount() / 100)));
                }
            } else {
                tv_zonge01.setText(TextViewUtil.getSumLength(transferProductInfoBean.getDebt().getDebtAmount()));
            }
            tv_time.setText(transferProductInfoBean.getDebt().getRemainTerm() + "");
            tv_shengyu.setText(NumberFormat.getInstance().format(bid.getApr()) + "%");
            //基本信息
            tv_object_name.setText(bid.getTitle());
            tv_nian_shouyilv.setText(NumberFormat.getInstance().format(bid.getApr()) + "%");
            tv_rongzi_money.setText(NumberFormat.getInstance().format(bid.getAmount() / 10000) + " 万");   ///融资近额  100万
            tv_danbao_organization.setText(bid.getAgencyName());                //担保机构
            tv_huankuan_style.setText(bid.getRepayment().getName());

            //借款人信息id.substring(0, 3) + "********" + id.substring(14);
            tv_borrower_name.setText(bid.getUser().getRealityName().substring(0, 1) + "**");
            tv_id_number.setText(bid.getUser().getIdNumber().substring(0, 6) + "*******"
                    + bid.getUser().getIdNumber().substring(bid.getUser().getIdNumber().length()));
            tv_sex.setText(bid.getUser().getSex());
            tv_age.setText(bid.getUser().getAge() + "");
            tv_marital_status.setText(bid.getUser().getMaritalName());
            tv_current_city.setText(bid.getUser().getAddress());

            //房产信息
            if (bid.getLocation() != null && bid.getLocation().length() >= 8) {
                tv_property_located.setText(bid.getLocation().substring(0, 8) + "********");
            } else {
                tv_property_located.setText(bid.getLocation());
            }
            tv_building_area.setText(bid.getBuildArea() + "㎡");
            tv_evaluated_price.setText(bid.getAssessmentPrice());

            //风控信息
            risk_info.setText(Html.fromHtml(bid.getRiskInfo()));
            List<String> page = transferProductInfoBean.getAudititems();
            saMenuItem.setList(page);
            saMenuItem.notifyDataSetChanged();

        }
    }

    /**
     * 转让底部按钮的判断
     ***/
    private void zhuanButtonPanduan() {
        bt_mairu.setVisibility(View.VISIBLE);
        if (transferProductInfoBean.getHavCoupons() > 0) {
            bt_mairu.setText("购买时已使用兜券，不可转让");
            bt_mairu.setBackgroundColor(0xFFCCCCCC);
            bt_mairu.setTextSize(15);
        } else if (transferProductInfoBean.getLockTranfer() > 0) {
            bt_mairu.setText("锁定期" + transferProductInfoBean.getLockTimeBefore() + "-" + transferProductInfoBean.getLockTimeAfter());
            bt_mairu.setBackgroundColor(0xFFCCCCCC);
            bt_mairu.setTextSize(15);
        } else {
            bt_mairu.setText("转让");
            bt_mairu.setBackgroundColor(0xFFF77F96);
            bt_mairu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (transferProductInfoBean.getTransferLimit() > 0) {
                        ToastManager.showShort(TransferProductInfo.this, "标的还款日，无法发起转让");
                    } else if (transferProductInfoBean.getOverdue() > 0) {
                        ToastManager.showShort(TransferProductInfo.this, "该标的已逾期，无法发起转让");
                    } else {
                        Intent intent = new Intent(TransferProductInfo.this, TransferProInfoResult.class);
                        intent.putExtra("investId", transferProductInfoBean.getInvest().getId() + "");
                        startActivity(intent);
                    }
                }
            });
        }
    }

    /**
     * 详情页的网络请求
     */
    private void loginRequest() {
        if (!DataHandler.isNetworkConnected(this)) {
            loadbox.failed();
            ToastManager.show(this, "请检查网络连接");
            return;
        }
        final Map<String, String> parameters = DataHandler.getParameters("1206");
        parameters.put("investId", investId);
        if (user != null && rettonStyle.equals("1")) {
            parameters.put("id", user.getCurrUser().getSign());
        }
        JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters), null, succeedRespon, DataHandler.getEor(this, loadbox));
        request.setRetryPolicy(new DefaultRetryPolicy(ConstantValues.TIMEOUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        requen.add(request);
    }

    /**
     * 取消转让的网络请求
     */
    private void quxiaoZhuanrangRequest() {
        final Map<String, String> parameters = DataHandler.getParameters("1208");
        if (user != null) {
            parameters.put("debtId", transferProductInfoBean.getDebt().getId() + "");
            parameters.put("id", user.getCurrUser().getSign());
        }
        JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters), null, succeedRespon1, null);
        requen.add(request);
    }

    private Response.Listener<JSONObject> succeedRespon1 = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jobj) {
            L.i(jobj.toString());
            if (JSONManager.getError(jobj) == -1) {
                UIManager.showSuccessDilog(TransferProductInfo.this, R.drawable.img_douhappy, 0, "恭喜您，取消转让成功", "我知道了", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActivityManager.getInstance().exitActivity();
                        Intent intent = new Intent(TransferProductInfo.this, MyInvestListAct.class);
                        Bundle mBundle = new Bundle();
                        mBundle.putInt("xItem", 0);
                        intent.putExtras(mBundle);
                        startActivity(intent);
                    }
                });
            } else {
                ToastManager.showShort(TransferProductInfo.this, JSONManager.getMsg(jobj));
            }
        }
    };
    private Response.Listener<JSONObject> succeedRespon = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jobj) {
            L.i(jobj.toString());
            if (JSONManager.getError(jobj) == -1) {
                initData(jobj);
            } else {
                loadbox.failed();
                ToastManager.showShort(TransferProductInfo.this, JSONManager.getMsg(jobj));
            }
        }
    };

    /**
    * 数据展示
    * */
    protected void initData(JSONObject jobj) {
        list.clear();
        adapter.notifyDataSetChanged();
        loadbox.success();
        findViewById(R.id.invest_details_mc).setVisibility(View.VISIBLE);//在数据调用成功后显示页面内容
        transferProductInfoBean = JSON.parseObject(jobj.toString(), TransferProductInfoBean.class);
//        Log.i("解析过得文件", transferProductInfoBean.toString());
        List<InvestBills> listdata = transferProductInfoBean.getInvestBills();
        if (listdata != null) {
            for (InvestBills item : listdata) {
                InvestBills item2 = new InvestBills();
                item2.setPeriods(item.getPeriods());
                item2.setReceive_amount(item.getReceive_amount());
                item2.setReceive_corpus(item.getReceive_corpus());
                item2.setReal_receive_corpus(item.getReal_receive_corpus());
                item2.setReceive_interest(item.getReceive_interest());
                item2.setReal_receive_interest(item.getReal_receive_interest());
                item2.setReceive_time(item.getReceive_time());
                item2.setReal_receive_time(item.getReal_receive_time());
                item2.setStatus(item.getStatus());
                item2.setIsben(true);
                if (transferProductInfoBean.getJsonprepayment().getPrepaymentstatus() > 0) { //提前还款
                    if (item.getReal_receive_interest() != 0)
                        list.add(item);
                    if (item.getReal_receive_corpus() != 0)
                        list.add(item2);
                } else {
                    if (item.getReceive_interest() != 0)
                        list.add(item);
                    if (item.getReceive_corpus() != 0)
                        list.add(item2);
                }
            }
            adapter.notifyDataSetChanged();
        }
        upData();
    }

    /**
     * ****************  倒计时 **************
     */
    public void daoJiShi() {
        long diff = transferProductInfoBean.getDebt().getEndSecond();
        L.d("TransferProductInfo倒计时====================" + diff);
        long[] time;
        if (diff <= 0) {
            time = new long[]{0, 0, 0};
        } else {
            long hours = diff / (60 * 60);
            long minutes = (diff % (60 * 60)) / 60;
            long second = (diff % (60 * 60) % 60);
            time = new long[]{hours, minutes, second};
        }
        timeDownView.setTimes(time);
        if (!timeDownView.isRun()) {
            timeDownView.run();
        }
    }


    /**
     * **************回款计划********************
     */
    public void huiKuanJiHua() {
        if (transferProductInfoBean.getInvestBills() != null) {
            huikuanjihua.setVisibility(View.VISIBLE);
            if (transferProductInfoBean.getJsonprepayment().getPrepaymentstatus() != 0) {
                tv_error_hint.setVisibility(View.VISIBLE);
                tv_error_hint.setText("*借款人于" + transferProductInfoBean.getJsonprepayment().getYear() + "年" +
                        transferProductInfoBean.getJsonprepayment().getMonth() + "月" + transferProductInfoBean.getJsonprepayment().getDay() + "日提前还本" +
                        TextViewUtil.getSumLength(transferProductInfoBean.getJsonprepayment().getSunmoney()) + "元，罚息" +
                        TextViewUtil.getSumLength(transferProductInfoBean.getJsonprepayment().getDefaultinterest()) + "元");
            }
        }
    }

    //禁止scorll自动滑动到底部
    private void disableAutoScrollToBottom() {
        sv_include.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
        sv_include.setFocusable(true);
        sv_include.setFocusableInTouchMode(true);
        sv_include.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.requestFocusFromTouch();
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (!NewUtil.checkClick(v.getId())) return;
        User user = AccountManager.getInstance().getUser();
        String url;
        Intent intent = new Intent(this, BaseWebActivity.class);
        Bundle bundle = new Bundle();
        switch (v.getId()) {
            case R.id.loadStatusBox:
                //刷新请求
                loginRequest();
                break;
            case R.id.zhuan_mingxi:
                Intent intent0 = new Intent(this, ZhuanrangMingxi.class);
                bundle.putString("investId", investId);
                intent0.putExtras(bundle);
                startActivity(intent0);
                break;
            case R.id.borrow_hetong:
                url = DataHandler.WEBURL + "/front/wa/querycompact?investId=" + investId + "&compacttype=3&id=" + user.getCurrUser().getSign();
                bundle.putString("url", url);
                bundle.putString("title", "借款合同");
                intent.putExtras(bundle);
                startActivity(intent);
                clossPopupWindow();
                break;
            case R.id.jujian_xieyi:
                url = DataHandler.WEBURL + "/front/wa/querycompact?investId=" + investId + "&compacttype=0&id=" + user.getCurrUser().getSign();
                bundle.putString("url", url);
                bundle.putString("title", "居间协议");
                intent.putExtras(bundle);
                startActivity(intent);
                clossPopupWindow();
                break;
            case R.id.zhuanrang_hetong:
                url = DataHandler.WEBURL + "/front/wa/querycompact?investId=" + investId + "&compacttype=2&id=" + user.getCurrUser().getSign();
                bundle.putString("url", url);
                bundle.putString("title", "转让合同");
                intent.putExtras(bundle);
                startActivity(intent);
                clossPopupWindow();
                break;
        }
    }
}
